<template>
  <div>
    <el-dialog
      title="绑定手机号"
      :visible.sync="bindPhoneDialogVisible"
      width="35%"
      :modal="false"
      :top="'15%'"
      @close="handleBindPhoneDialogClose"
    >
      <el-form ref="bindPhoneForm" :model="bindPhoneForm" label-width="80px">
        <el-form-item label="手机号" prop="phonenumber">
          <el-input v-model="bindPhoneForm.phonenumber"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="verificationCode">
          <el-input v-model="bindPhoneForm.verificationCode"></el-input>
          <el-button
            :disabled="countdownBind > 0"
            @click="sendBindPhoneVerificationCode"
            style="margin-top: 10px"
          >
            {{
              countdownBind > 0 ? `${countdownBind}秒后重新发送` : "发送验证码"
            }}
          </el-button>
        </el-form-item>
        <!-- 其他相关字段和操作 -->
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleBindPhoneDialogClose">取消</el-button>
        <el-button type="primary" @click="submitBindPhoneForm">确定</el-button>
      </span>
    </el-dialog>
    <!-- You may want to include the UpdatePassWordDialog component here if needed -->
  </div>
</template>

<script>
// Import any necessary components or libraries

export default {
  data() {
    return {
      bindPhoneDialogVisible: false,
      bindPhoneForm: {
        phonenumber: "",
        verificationCode: "",
        // 其他相关字段
      },
      countdownBind: 0,
    };
  },

  methods: {
    showBindPhoneDialog() {
      this.bindPhoneDialogVisible = true;
    },
    handleBindPhoneDialogClose() {
      this.bindPhoneDialogVisible = false;
      // 清空表单数据
      this.$refs.bindPhoneForm.resetFields();
      // 重置倒计时
      this.countdownBind = 0;
    },
    sendBindPhoneVerificationCode() {
      // 模拟发送验证码的后台逻辑
      console.log("发送验证码");

      const verifyPhoneNumber = /^1[3456789]\d{9}$/;
      // 验证手机号合法性
      if (!verifyPhoneNumber.test(this.bindPhoneForm.phonenumber)) {
        this.$message({
          message: "手机号不合法，请输入11位的数字的手机号",
          type: "error",
        });
        return false;
      }

      // 发送验证码请求
      // Replace this with your actual API call
      this.$request.getHttp({
        url: "/sms/sendMessage",
        params: {
          number: this.bindPhoneForm.phonenumber,
          prefix: "binding",
        },
        success: (data) => {
          console.log(data);
        },
        error: (error) => {},
      });

      // 开始倒计时
      this.countdownBind = 60;
      const timer = setInterval(() => {
        if (this.countdownBind > 0) {
          this.countdownBind--;
        } else {
          clearInterval(timer);
        }
      }, 1000);
    },
    submitBindPhoneForm() {
      // 校验格式
      if (!this.verifyBindPhoneFormat()) {
        return;
      }

      // 验证验证码是否正确
      this.$request.postHttp({
        url: "/verificationcode/checkCodeWithPrefix",
        data: {
          phonenumber: this.bindPhoneForm.phonenumber,
          verificationCode: this.bindPhoneForm.verificationCode,
          prefix: "binding",
        },
        success: (data) => {
          if (data.code == 200) {
            if (!data.pass) {
              return;
            }
            //通过验证，根据用户id将手机号绑定到用户
            this.updatePhoneNumberByUserId();
          } else if (data.code == 500) {
            this.$message({
              message: data.msg,
              type: "error",
            });
          }
        },
        error: (error) => {},
      });
    },
    verifyBindPhoneFormat() {
      const verifyPhoneNumber = /^1[3456789]\d{9}$/;
      const verifyCode = /^\d{6}$/;

      // 验证手机号合法性
      if (!verifyPhoneNumber.test(this.bindPhoneForm.phonenumber)) {
        this.$message({
          message: "手机号不合法，请输入11位的数字的手机号",
          type: "error",
        });
        return false;
      }
      // 验证验证码合法性
      if (!verifyCode.test(this.bindPhoneForm.verificationCode)) {
        this.$message({
          message: "请输入6位数字的验证码",
          type: "error",
        });
        return false;
      }
      return true;
    },
    // 根据用户id修改手机号
    updatePhoneNumberByUserId() {
      this.$request.postHttp({
        url: "/user/updatePhoneNumberByUserId",
        data: {
          userid: localStorage.getItem("userId"),
          phonenumber: this.bindPhoneForm.phonenumber,
        },
        success: (data) => {
          if (data.code == 500) {
            this.$message({
              message: data.msg,
              type: "error",
            });
            return;
          }

          if (data.code == 200) {
            this.$message({
              message: "修改成功！",
              type: "success",
            });
          }
          //关闭对话框
          this.bindPhoneDialogVisible = false;
          //刷新手机号
          this.$emit("getPhoneNumberByUserId");
        },
        error: (error) => {},
      });
    },
  },
};
</script>

<style scoped>
</style>
